package com.mengdie.shuidi.ui.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mengdie.shuidi.AppManager;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.model.LineEntity;
import com.mengdie.shuidi.model.NewLineMode;
import com.mengdie.shuidi.ui.activity.ProvinceActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by apple on 16/10/14.
 */

public class ProvinceAdapter extends BaseQuickAdapter<NewLineMode.RetDataBean, BaseViewHolder> {

    private List<NewLineMode.RetDataBean> list;

    public ProvinceAdapter(List<NewLineMode.RetDataBean> data) {
        super(R.layout.item_province, data);
        this.list = data;
    }

    @Override
    protected void convert(BaseViewHolder holder, NewLineMode.RetDataBean item) {
        holder.setText(R.id.tv_province, item.getProvince());
        ImageView imageView = holder.getView(R.id.iv_line_province);
        //子类
        RecyclerView recyclerViewChild = holder.getView(R.id.recycleview_child);

        if (item.isShow()) {
            imageView.setImageResource(R.drawable.line_up);
            recyclerViewChild.setVisibility(View.VISIBLE);
            recyclerViewChild.setLayoutManager(new GridLayoutManager(mContext, 2));

            List<NewLineMode.RetDataBean.ServListBean> cityList = item.getServ_list();
            CityAdapter adapter = new CityAdapter(cityList);
            recyclerViewChild.setAdapter(adapter);

            recyclerViewChild.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    super.onItemClick(adapter, view, position);
                    NewLineMode.RetDataBean.ServListBean cityBean = (NewLineMode.RetDataBean.ServListBean) adapter.getData().get(position);

                    String name = cityBean.getName();
                    if (TextUtils.isEmpty(name)) {
                        return;
                    }
//                    String isOpen = MDModel.getInstance().getIsOpen();
//                    if (EmptyUtils.isEmpty(isOpen)) {
//                        EventBus.getDefault().post(new LineEvent("", name, cityBean.getIp() + ":" + cityBean.getNewPort(), cityBean.getId(),
//                                cityBean.getAgreeType(), "1", null));
//                    }else{
//                        if (isOpen.equals("1")) {
//                            EventBus.getDefault().post(new LineEvent("", name, cityBean.getProxyIp() + ":" + cityBean.getProxyPort(), cityBean.getId(),
//                                    cityBean.getAgreeType(), "1", null));
//                        }else{
//                            EventBus.getDefault().post(new LineEvent("", name, cityBean.getIp() + ":" + cityBean.getNewPort(), cityBean.getId(),
//                                    cityBean.getAgreeType(), "1", null));
//                        }
//                    }
                    LineEntity line = new LineEntity();
                    line.setIp(cityBean.getIp());
                    line.setNewPort(cityBean.getNewPort());
                    line.setProxyIp(cityBean.getProxyIp());
                    line.setProxyPort(cityBean.getProxyPort());
                    line.setName(cityBean.getName());
                    EventBus.getDefault().post(line);
                    AppManager.getAppManager().finishActivity(ProvinceActivity.class);
                }

                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                }
            });
        } else {
            imageView.setImageResource(R.drawable.line_down);
            recyclerViewChild.setVisibility(View.GONE);
        }

    }

}
